package com.fsi.client;

import com.fsi.client.util.MyDate;
import com.fsi.client.util.MyList;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class EditCustomerGUI extends ServiceGUI {
	private static int imageCount = 0;

	private final Label title = new Label("Edit Customer Page");
	private final VerticalPanel photoPanel;
	private final Image photo;

	private final VerticalPanel generalDetailsPanel;
	private final Grid generalGrid;
	private final ListBox smokerEd;
	private final TextArea descriptionEd;
	private final TextArea firstDateEd;
	private final Button saveGeneralBtn;

	private final VerticalPanel fixedDetailsPanel;
	private final Grid fixedGrid;
	private final TextBox emailAddressEd;
	private final TextBox userNameEd;
	private final MyDate birthdateEd;
	private final MyList genderEd;
	private final MyList countryEd;
	private final TextBox postCodeEd;
	private final Button saveFixedBtn;

	private String loginEmail;
	private Customer customer;

	public void setLoginEmail(String loginEmail) {
		this.loginEmail = loginEmail;
	}

	public EditCustomerGUI() {
		photoPanel = new VerticalPanel();
		photo = new Image();
		photo.setHeight("150px");

		generalDetailsPanel = new VerticalPanel();
		generalGrid = new Grid(3, 2);
		String[] s = { "k", "h" };
		smokerEd = new ListBox();
		smokerEd.addItem("Yes");
		smokerEd.addItem("No");
		descriptionEd = new TextArea();
		firstDateEd = new TextArea();
		saveGeneralBtn = new Button("Save General");

		userNameEd = new TextBox();
		emailAddressEd = new TextBox();
		birthdateEd = new MyDate();
		genderEd = new MyList(Customer.GENDERS);
		countryEd = new MyList(Customer.COUNTRIES);
		postCodeEd = new TextBox();
		saveFixedBtn = new Button("Save Fixed");
		fixedGrid = new Grid(6, 2);
		fixedDetailsPanel = new VerticalPanel();

		buildForm();
		generalDetailsPanel.add(generalGrid);
		generalDetailsPanel.add(saveGeneralBtn);

		fixedDetailsPanel.add(fixedGrid);
		fixedDetailsPanel.add(saveFixedBtn);

		this.add(title);
		this.add(photoPanel);
		this.add(generalDetailsPanel);
		this.add(fixedDetailsPanel);

		wireEvents();
	}

	private void wireEvents() {
		saveFixedBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				saveFixed();
			}
		});

		saveGeneralBtn.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				saveGeneral();
			}

		});
	}

	private void buildForm() {
		photoPanel.add(photo);

		generalGrid.setWidget(0, 0, new Label("Smoker"));
		generalGrid.setWidget(0, 1, smokerEd);

		generalGrid.setWidget(1, 0, new Label("Description"));
		generalGrid.setWidget(1, 1, descriptionEd);

		generalGrid.setWidget(2, 0, new Label("First Date"));
		generalGrid.setWidget(2, 1, firstDateEd);

		fixedGrid.setWidget(0, 0, new Label("User Name"));
		fixedGrid.setWidget(0, 1, userNameEd);

		fixedGrid.setWidget(1, 0, new Label("Email Address"));
		fixedGrid.setWidget(1, 1, emailAddressEd);

		fixedGrid.setWidget(2, 0, new Label("Birthdate"));
		fixedGrid.setWidget(2, 1, birthdateEd);

		fixedGrid.setWidget(3, 0, new Label("Gender"));
		fixedGrid.setWidget(3, 1, genderEd);

		fixedGrid.setWidget(4, 0, new Label("Country"));
		fixedGrid.setWidget(4, 1, countryEd);

		fixedGrid.setWidget(5, 0, new Label("Postcode"));
		fixedGrid.setWidget(5, 1, postCodeEd);
	}

	private void saveFixed() {
		customer.setUserName(userNameEd.getText().trim());
		customer.setEmailAddress(emailAddressEd.getText().trim());
		customer.setGender(genderEd.getSelectedIndex());
		customer.setCountry(countryEd.getSelectedIndex());
		customer.setPostCode(postCodeEd.getText().trim());
		customer.setBirthdate(birthdateEd.getDate());
		saveFixedBtn.setEnabled(false);
		service.updateFixedCustomer(this, customer);
	}

	private void saveGeneral() {
		customer.setSmoker(smokerEd.getSelectedIndex() == 0);
		customer.setDescription(descriptionEd.getText());
		customer.setFirstDate(firstDateEd.getText());

		saveGeneralBtn.setEnabled(false);
		service.updateGeneralDetails(this, customer);
	}

	void setEmail(String email) {
		emailAddressEd.setText(email);
	}

	// ****************************
	// ************ service methods
	// ****************************

	public void loadDetails(Customer currentCustomer) {
		customer = currentCustomer;
		photo.setUrl(GWT.getModuleBaseURL() + "getphoto?nick="
				+ customer.getPhotoID() + "&variable=" + imageCount++);

		if (customer.isSmoker()) {
			smokerEd.setItemSelected(0, true);
		} else {
			smokerEd.setItemSelected(1, true);
		}
		descriptionEd.setText(customer.getDescription());
		firstDateEd.setText(customer.getFirstDate());

		userNameEd.setText(customer.getUserName());
		emailAddressEd.setText(customer.getEmailAddress());
		birthdateEd.setDate(customer.getBirthdate());
		int g = customer.getGender();
		if (customer.getGender() > -1)
			genderEd.setItemSelected(customer.getGender(), true);
		if (customer.getCountry() > -1)
			countryEd.setItemSelected(customer.getCountry(), true);
		postCodeEd.setText(customer.getPostCode());
	}

	@Override
	public void service_afterUpdateFixed() {
		saveFixedBtn.setEnabled(true);
		gui.setStatus("Fixed details updated");
	}

	@Override
	public void service_updateFixedFailed(String message) {
		saveFixedBtn.setEnabled(true);
		gui.setStatus("Error saving fixed details: " + message);
	}

	@Override
	public void service_updateGeneralFailed(String message) {
		saveGeneralBtn.setEnabled(true);
		gui.setStatus("Error saving general details: " + message);
	}

	@Override
	public void service_afterUpdateGeneral() {
		saveGeneralBtn.setEnabled(true);
		gui.setStatus("General details updated");
	};

}
